<template>
  <div class="MyFavorite-contain">
    <div class="descriptions-container">
      <el-card v-if="articleList.length != 0">
        <div v-for="(item, index) in articleList" :key="item.id">
          <!-- 文章内容区域 -->
          <div style="display: flex;">
            <!-- 封面图片区域 -->
            <div style=" width: auto;margin-right: 20px; display: flex;justify-content: center;align-items: center;">
              <img v-if="item.picture" :src="item.picture" class="avatar">
            </div>
            <!-- 文字区域 -->
            <div style=" width: auto;">
              <el-descriptions :colon="false" direction="vertical" :column="1">
                <el-descriptions-item>
                  <p @click="$router.push('/articleDetail?articleID=' + item.id)"
                    style="margin: 5px 0; font-weight: bolder;font-size: 18px; cursor: pointer;">
                    {{ index + 1 }}、 {{ item.title }}
                  </p>
                </el-descriptions-item>
                <el-descriptions-item>
                  <p @click="$router.push('/articleDetail?articleID=' + item.id)"
                    style="margin: 5px 0;padding-bottom: 14px; font-size: 12px; width: auto;cursor: pointer;">
                    {{ item.content }}
                  </p>
                </el-descriptions-item>
                <el-descriptions-item>
                  <p style="margin: 0;font-size: small;cursor: pointer;" @click="DeleteArticle(item.articleID)">
                    <i style="margin-right: 3px;" class="el-icon-delete"></i>删除
                  </p>
                </el-descriptions-item>
              </el-descriptions>
            </div>

          </div>
          <!-- 分割线 -->
          <el-divider></el-divider>
        </div>
      </el-card>
    </div>
    <div class="empty-contain">
      <span v-if="articleList.length == 0 && !(article.searchContent == ''||article.searchContent == null)" style="display: flex;justify-content: center">
        <p style="padding-right: 100px;color: #ccc;">未找到对应文章</p>
      </span>
      <span v-if="articleList.length == 0 && (article.searchContent == ''||article.searchContent == null)">
        <el-empty description="您还没有收藏过文章..."></el-empty>
      </span>
    </div>
  </div>
</template>

<script>
import { getMyFavoriteAPI, deleteMyFavoriteAPI } from '@/api/myFavorite'
export default {
  data() {
    return {
      article: {
        searchContent: '',
      },
      articleList: [],
    }
  },
  mounted() {
    this.article.searchContent = this.$route.query.searchContent
    this.getMyFavoriteList()
  },
  methods: {
    // 删除文章
    DeleteArticle(id) {
      this.$confirm("你确定要删除吗？").then(async () => {
        const res = await deleteMyFavoriteAPI(id)
        this.getMyFavoriteList()
        if (res.code == 200) {
          this.$message.success("删除成功")
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.$message.info("已取消")
      })
    },
    // 获取文章列表
    async getMyFavoriteList() {
      const res = await getMyFavoriteAPI(this.article)
      if (res.code == 200) {
        this.articleList = res.data.rows
      }
    }
  }
}
</script>

<style>
.MyFavorite-contain {
  /* width: 100%; */

  .avatar {
    width: 90px;
    height: 90px;
    border-radius: 8px;
  }

  .descriptions-container {
    .el-descriptions :not(.is-bordered) .el-descriptions-item__cell {
      padding-bottom: 0px;
    }

    .el-descriptions__body .el-descriptions__table .el-descriptions-item__cell {
      max-width: 200px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
    }


    .el-divider--horizontal {
      margin: 12px 0;
    }

  }
}
</style>
